<?php
echo $this->Html->script(array('jquery.checkboxtree'), array('inline'=> false));
echo $this->Html->css(array('jquery.checkboxtree'),null, array('inline'=> false));
$this->start('toolbar');
echo $this->Html->link(
	'<i class="icon-ok icon-white"></i>&nbsp;'.__('Save'),
	'javascript:;',
	array('escape'=> false, 'class'=> 'btn btn-small btn-success', 'onclick'=> "OWS.submitbutton('save')")
);
echo $this->Html->link(
	'<i class="icon-plus"></i>&nbsp;'.__('Save & New'),
	'javascript:;',
	array('escape'=> false, 'class'=> 'btn btn-small btn-info', 'onclick'=> "OWS.submitbutton('save2new')")
);
echo $this->Html->link(
	'<i class="icon-remove"></i>&nbsp;'.__('Cancel'),
	array('controller'=> 'users', 'action'=> 'index'),
	array('escape'=> false, 'class'=> 'btn btn-small')
);
$this->end();
$datetimeFormat = Configure::read('Setting.datetime_format');
?>
<?php 
echo $this->Form->create('User', array(
    'inputDefaults' => array(
        'div' => 'control-group',
        'label' => array(
            'class' => 'control-label'
        ),
        'wrapInput' => 'controls'
    ),
    'class' => 'form-validate form-horizontal',
    'novalidate' => true,
    'id'=> 'user_form',
));
?>
<ul class="nav nav-tabs">
	<li class="active"><a href="#detail" data-toggle="tab"><?php echo __('Account Details') ?></a></li>
	<li><a href="#subject" data-toggle="tab"><?php echo __('Assigned Subject') ?></a></li>
</ul>
<div class="tab-content">
	<div class="tab-pane active" id="detail">
        <?php
        echo $this->Form->input('fullname', array(
            'label' => array(
                'text' => __('Name') . '<span class="star">*</span>'
            ),
        ));
        echo $this->Form->input('username', array(
            'label' => array(
                'text' => __('Username') . '<span class="star">*</span>'
            ),
        ));
        echo $this->Form->input('passwd', array(
            'type' => 'password',
            'label' => array(
                'text' => __('Password')
            ),
        ));
        echo $this->Form->input('passwd_confirm', array(
            'type' => 'password',
            'label' => array(
                'text' => __('Password Confirm')
            ),
        ));
        echo $this->Form->input('email', array(
            'label' => array(
                'text' => __('Email') . '<span class="star">*</span>'
            ),
        ));
        echo $this->Form->input('role_id', array(
            'empty'=> false,
            'label' => array(
                'text' => __('Role') . '<span class="star">*</span>'
            ),
            'options' => $roles,
            'value'=> $this->data['User']['role_id']
        ));
        echo $this->Form->input('created', array(
            'type' => 'text',
            'label' => array(
                'text' => __('Register Date')
            ),
            'class'=> 'readonly',
            'readonly'=> true,
            'value'=> date($datetimeFormat, strtotime($this->data['User']['created']))
        ));
        echo $this->Form->input('last_login_at', array(
            'type' => 'text',
            'label' => array(
                'text' => __('Last Visit Date')
            ),
            'class'=> 'readonly',
            'readonly'=> true,
            'value'=> date($datetimeFormat, strtotime($this->data['User']['last_login_at']))
        ));
        echo $this->Form->input('status', array(
            'empty'=> false,
            'label' => array(
                'text' => __('Status')
            ),
            'options' => Configure::read('Constant.status'),
            'value'=> $this->data['User']['role_id']
        ));
        ?>
    </div>
    <div class="tab-pane" id="subject">
        <div class="control-group">
			<div class="controls"><?php
			if(isset($this->request->data['User']['subject'])){
				$selectedSubjectIds = $this->request->data['User']['subject'];
			}else{
				$selectedSubjectIds = array();
			}
			
			//TODO: chinh javascript khi select tat ca subject
			echo $this->Layout->generateSubjectTree($subjects, $selectedSubjectIds);
			?></div>
		</div>
    </div>
</div>
<?php echo $this->Form->hidden('task', array('id'=> 'task'))?>
<?php echo $this->Form->end() ?>
<script type="text/javascript">
$("#subject > .control-group > .controls > ul").checkboxTree({
    onCheck: {
    	ancestors:'checkIfFull',
    	descendants: 'check'
    },
    onUncheck: {
        ancestors: 'uncheck'
    }
});
OWS.submitbutton = function(task) {
	OWS.submitform(task, document.getElementById('user_form'));
};
</script>